package com.atguigu.gulimall.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class GulimailCorsConfiguration {
    @Bean
    public CorsWebFilter corsWebFilter(){
        // CorsConfigurationSource 因为这个类是一个接口 实现它的类是 UrlBasedCorsConfigurationSource
//        CorsConfigurationSource corsConfigurationSource=new CorsConfigurationSource() {
//            @Override
//            public CorsConfiguration getCorsConfiguration(ServerWebExchange serverWebExchange) {
//                return null;
//            }
//        };
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration=new CorsConfiguration(); //所有跨域 都写在这里面
        corsConfiguration.addAllowedHeader("*");  //允许哪些头部跨域，* 代表所有都可以
        corsConfiguration.addAllowedMethod("*");  // 允许那些请求方式进行跨域 * 代表所有 比如 get post···
        corsConfiguration.addAllowedOrigin("*"); // 任意请求来源 都允许跨域
        corsConfiguration.setAllowCredentials(true);// 是否允许携带 cooke
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }
}
